<template>
  <div>
    <!-- ==={{ permIds }} -->
    <el-tree
      ref="refTree"
      :data="perList"
      show-checkbox
      node-key="id"
      default-expand-all
      check-strictly
      :default-checked-keys="permIds"
      :props="defaultProps"
    />
    <div style="margin: 30px 0">
      <!-- 底部 -->
      <el-button size="small">取消</el-button>
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getPermissionList } from '@/api/permission.js'
import { getRoleDetail, assignPerm } from '@/api/setting.js'
import arrayToTree from 'array-to-tree'
export default {
  name: '',
  props: ['roleId'],
  data() {
    return {
      perList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permIds: []
    }
  },
  created() {
    this.getPer()
    this.getRight()
  },
  methods: {
    // 获取所有权限列表
    async getPer() {
      const res = await getPermissionList()
      // console.log(`res`, res)
      // this.prtList = res.data
      this.perList = arrayToTree(res.data, {
        parentProperty: 'pid',
        customID: 'id'
      })
      console.log(this.perList, 888999)
    },
    async getRight() {
      // 获取当前角色所拥有的权限
      const res = await getRoleDetail(this.roleId)
      // console.log(this.roleId, 111)
      console.log(`res1212`, res)
      this.permIds = res.data.permIds
      // 回填
      // this.$refs.refTree.setCheckedKeys(res.data.permIds)
    },
    async hSubmit() {
      /* 点击分配权限对话框中的确认按钮
    步骤分析：
    1-获取给角色分配的权限===获取复选框中选中的id值
    2-封装api函数
    3-调用函数
    4-成功后：关闭对话框
    */
      //  获取角色分配权限--获取复选框中选中的ID值
      var prtMissIds = this.$refs.refTree.getCheckedKeys() // []
      console.log(prtMissIds, 9988)
      // // 调用接口函数
      const res = await assignPerm({
        id: this.roleId,
        permIds: prtMissIds
      })
      this.$emit('success2')
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
